package cn.jinbyte.web.utils.helper;

import cn.hutool.http.useragent.UserAgentUtil;
import cn.jinbyte.core.utils.ServletUtils;
import cn.jinbyte.web.annotation.ApiLog;
import cn.jinbyte.web.model.AccessUser;
import cn.jinbyte.web.model.LogInclude;
import cn.jinbyte.web.model.LogRecord;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.web.method.HandlerMethod;

import java.util.Set;

/**
 * 日志工具类
 */
public class WebLogHelper {

    /**
     * 获取模块名称
     *
     * @param handlerMethod 目标方法
     * @param apiLog        接口日志注解
     * @return 模块名称
     */
    public static String logModule(HandlerMethod handlerMethod, ApiLog apiLog) {
        if (StringUtils.isNotBlank(apiLog.module())) {
            return apiLog.module();
        }
        return handlerMethod.getBeanType().getSimpleName();
    }

    /**
     * 获取请求信息
     *
     * @param request  请求
     * @param includes 包含信息
     * @return 请求信息
     */
    public static LogRecord.LogRequest logRequest(HttpServletRequest request, Set<LogInclude> includes) {
        LogRecord.LogRequest.LogRequestBuilder requestBuilder = LogRecord.LogRequest.builder()
                .method(request.getMethod())
                .uri(request.getRequestURI())
                .ip(ServletUtils.getClientIP(request))
                .address(request.getRemoteAddr())
                .userAgent(UserAgentUtil.parse(request.getHeader(HttpHeaders.USER_AGENT)));

        if (includes != null && !includes.isEmpty()) {
            if (includes.contains(LogInclude.REQUEST_HEADERS)) {
                requestBuilder.headers(ServletUtils.getHeaders(request));
            }
            if (includes.contains(LogInclude.REQUEST_PARAM)) {
                requestBuilder.param(ServletUtils.getParameters(request));
            }
            if (includes.contains(LogInclude.REQUEST_BODY)) {
                requestBuilder.body(WebRequestHelper.getBody(request));
            }
        }
        return requestBuilder.build();
    }

    /**
     * 获取响应信息
     *
     * @param response 响应
     * @param includes 包含信息
     * @return 响应信息
     */
    public static LogRecord.LogResponse logResponse(HttpServletResponse response, Set<LogInclude> includes) {
        LogRecord.LogResponse.LogResponseBuilder responseBuilder = LogRecord.LogResponse
                .builder()
                .status(response.getStatus());
        if (includes != null && !includes.isEmpty()) {
            if (includes.contains(LogInclude.RESPONSE_HEADERS)) {
                responseBuilder.headers(ServletUtils.getHeaders(response));
            }
//        if (includes.contains(LogInclude.RESPONSE_PARAM)) {
//            responseBuilder.param(ServletUtils.getParameters(response));
//        }
//        if (includes.contains(LogInclude.RESPONSE_BODY)) {
//            responseBuilder.body(ServletUtils.getBody(response));
//        }
        }
        return responseBuilder.build();
    }

    /**
     * 获取访问用户信息
     *
     * @param request 请求
     * @return 访问用户信息
     */
    public static AccessUser logAccessUser(HttpServletRequest request) {
        //todo
        return AccessUser.builder()

                .build();
    }
}
